package com.csi.controller.history;

import com.alibaba.fastjson2.JSON;
import com.csi.pojo.Result;
import com.csi.service.HistoryService;
import com.csi.service.impl.HistoryServiceImpl;
import com.csi.utils.DateStringFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

@WebServlet("/history/findAmountsByMonthController")
public class FindAmountsByMonthController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        Integer year = null;
        Integer month = null;

        try {
            year = Integer.valueOf(req.getParameter("year"));
            month = Integer.valueOf(req.getParameter("month"));
        } catch (NumberFormatException e) {
            Result<Map<Date, Double>> result = new Result<>();
            result.setCode(400);
            result.setMsg("参数转换异常");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        HistoryService historyService = new HistoryServiceImpl();

        Map<Date, Double> map = historyService.findAmountsByMonth(year, month);


        Result<Map<Date, Double>> result = new Result<>();

        result.setCode(200);
        result.setMsg("success");
        result.setDatas(map);

        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
